-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SD-22]Updated hero image theme according header style #516
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yeniatencio use states instead of custom JS. See this example - https://github.com/dpc-sdp/tide_core/blob/develop/modules/tide_landing_page/tide_landing_page.module#L218
} | ||
}, | ||
}; | ||
})(Drupal); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not want use js if it is can be done without it. Read the SD, I have mentioned about conditional logic. You will find other examples in landing page where we have used conditional logic states. Take example from there and try to work it out using states instead of adding this JS please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MdNadimHossain agreed. I've already using conditional logic for enable/disable the field_landing_page_hero_theme
as per the requirements. However, one of the requirements is to set the default value of one field based on the value of another field and the only Drupal States that can be applied to a form field element are:
- enabled
- disabled
- required
- optional
- visible
- invisible
- checked
- unchecked
- expanded
- collapsed
As you can notice the default value can't be changed using states. Also, all the examples in landing page module is for changing one of the states in the list above but a default value.
Therefore, that's why I ended up writing JS code for it. Hope it makes sense now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yeniatencio I have suggested some suggestions. Please have a look into the revised version and it should work in the same way -
(function (Drupal) {
"use strict";
Drupal.behaviors.heroImageTheme = {
attach: function (context, settings) {
const imageTheme = context.querySelector(
'select[data-drupal-selector^="edit-field-landing-page-hero-theme"]'
);
if (imageTheme !== null) {
setImageTheme();
const headerStyles = context.querySelectorAll(
'input[data-drupal-selector^="edit-header-style-options"]'
);
if (headerStyles.length > 0) {
headerStyles.forEach((style) => {
style.addEventListener("change", setImageTheme);
});
}
}
function setImageTheme() {
const selectedHeaderStyle = document.querySelector(
'input[data-drupal-selector^="edit-header-style-options"]:checked'
);
if (!selectedHeaderStyle) {
return; // Exit if no style option is selected.
}
let defaultHeaderStyle = selectedHeaderStyle.value;
imageTheme.value = defaultHeaderStyle === "fullwidth" ? "dark" : "light";
}
},
};
})(Drupal);
|
||
Drupal.behaviors.heroImageTheme = { | ||
attach: function (context, settings) { | ||
const imageTheme = document.querySelector( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use context to ensure that the behavior only attaches to elements in the current page context.
'input[data-drupal-selector^="edit-header-style-options"]' | ||
); | ||
|
||
if (headerStyles !== null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check if (headerStyles !== null) should instead check for headerStyles.length > 0. It’s better to check if any elements are found before adding listeners.
} | ||
|
||
function setImageTheme() { | ||
let defaultHeaderStyle = document.querySelector( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a guard clause to prevent this error in case no radio buttons are checked
if (headerStyles !== null) { | ||
headerStyles.forEach((style) => { | ||
style.addEventListener("change", () => { | ||
setImageTheme(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of repeatedly defining an anonymous function for style.addEventListener("change", ...), you can directly pass the reference to the setImageTheme function.
|
||
const headerStyles = document.querySelectorAll( | ||
'input[data-drupal-selector^="edit-header-style-options"]' | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The script accesses document multiple times. It’s a good practice to minimize global object access by caching elements that you reuse.
} | ||
} | ||
|
||
function setImageTheme() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest moving the setImageTheme() function outside the main attach function for clarity and better separation of logic.
Hi @MdNadimHossain , Thanks for the suggestions. I have updated my pr as you requested. |
Hey @MdNadimHossain , I have updated my pr because there is a bug present when using states. In our case it happens when a hero image is added, then the hero image theme dropdown was being disabled. |
hey @yeniatencio |
for JS itself.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey @yeniatencio
Please review my comments and provide the test code for this new feature
hey @yeniatencio , What I’ve changed is just a preliminary idea for your further work, and it’s not ready for a code review yet. I think there are still some places that need adjustments.
|
Jira
https://digital-vic.atlassian.net/browse/SD-22
BE testing link: https://nginx-php.pr-1711.content-vic.sdp4.sdp.vic.gov.au/
Problem/Motivation
We want to have these scenarios for customised header:
Fix
field_landing_page_hero_theme
to mandatory.Related PRs
Screenshots
TODO